package com.etool.commons.bean;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BeanUtils {
	
	
	/**
	 * @description 用于javabean，自动set属性,主要用于从request获取的参数set进javabean里面
	 * @param obj
	 * @param params {key：属性名,value：属性值}
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static List<String> setXXX(Object obj,Map<String,Object> params) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
	{
		//结果，把set进去的值放进去，用于和params对比，是否全部set进去了
		List<String> result=new ArrayList<String>();
		
		Method[] methods = obj.getClass().getDeclaredMethods();
		
		Map<String, Method> methodNames=new HashMap<String, Method>();
		
		for(Method m:methods)
		{
			methodNames.put(m.getName(), m);
		}
		
		Set<String> keys = params.keySet();
		
		for(String key:keys)
		{
			String setfieldName="set"+key.trim().substring(0,1).toUpperCase()+key.trim().substring(1, key.length());
			if(methodNames.get(setfieldName)!=null)
			{
				methodNames.get(setfieldName).invoke(obj, params.get(key));
				result.add(key);
			}
		}
		
		return result;
	}
}
